﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class ProductCenter
    {
        public int Add(Chain.Model.ProductCenter model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into ProductCenter(");
            builder.Append("ProductName,ProductPhoto,ProductDesc,ProductCreateTime)");
            builder.Append(" values (");
            builder.Append("@ProductName,@ProductPhoto,@ProductDesc,@ProductCreateTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@ProductPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@ProductDesc", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@ProductCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.ProductName;
            cmdParms[1].Value = model.ProductPhoto;
            cmdParms[2].Value = model.ProductDesc;
            cmdParms[3].Value = model.ProductCreateTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int ProductID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from ProductCenter ");
            builder.Append(" where ProductID=@ProductID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProductID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProductID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ProductIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from ProductCenter ");
            builder.Append(" where ProductID in (" + ProductIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ProductID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from ProductCenter");
            builder.Append(" where ProductID=@ProductID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProductID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProductID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ProductID,ProductName,ProductPhoto,ProductDesc,ProductCreateTime ");
            builder.Append(" FROM ProductCenter ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ProductID,ProductName,ProductPhoto,ProductDesc,ProductCreateTime ");
            builder.Append(" FROM ProductCenter ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ProductID desc");
            }
            builder.Append(")AS Row, T.*  from ProductCenter T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.ProductCenter GetModel(int ProductID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ProductID,ProductName,ProductPhoto,ProductDesc,ProductCreateTime from ProductCenter ");
            builder.Append(" where ProductID=@ProductID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProductID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ProductID;
            Chain.Model.ProductCenter center = new Chain.Model.ProductCenter();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ProductID"] != null) && (set.Tables[0].Rows[0]["ProductID"].ToString() != ""))
            {
                center.ProductID = int.Parse(set.Tables[0].Rows[0]["ProductID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ProductName"] != null) && (set.Tables[0].Rows[0]["ProductName"].ToString() != ""))
            {
                center.ProductName = set.Tables[0].Rows[0]["ProductName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ProductPhoto"] != null) && (set.Tables[0].Rows[0]["ProductPhoto"].ToString() != ""))
            {
                center.ProductPhoto = set.Tables[0].Rows[0]["ProductPhoto"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ProductDesc"] != null) && (set.Tables[0].Rows[0]["ProductDesc"].ToString() != ""))
            {
                center.ProductDesc = set.Tables[0].Rows[0]["ProductDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["ProductCreateTime"] != null) && (set.Tables[0].Rows[0]["ProductCreateTime"].ToString() != ""))
            {
                center.ProductCreateTime = new DateTime?(DateTime.Parse(set.Tables[0].Rows[0]["ProductCreateTime"].ToString()));
            }
            return center;
        }

        public DataSet GetProductCenterInfo(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "ProductCenter";
            string[] column = new string[] { "ProductID,ProductName,ProductPhoto,ProductDesc,ProductCreateTime" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ProductID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM ProductCenter ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.ProductCenter model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update ProductCenter set ");
            builder.Append("ProductName=@ProductName,");
            builder.Append("ProductPhoto=@ProductPhoto,");
            builder.Append("ProductDesc=@ProductDesc,");
            builder.Append("ProductCreateTime=@ProductCreateTime");
            builder.Append(" where ProductID=@ProductID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@ProductPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@ProductDesc", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@ProductCreateTime", SqlDbType.DateTime), new SqlParameter("@ProductID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ProductName;
            cmdParms[1].Value = model.ProductPhoto;
            cmdParms[2].Value = model.ProductDesc;
            cmdParms[3].Value = model.ProductCreateTime;
            cmdParms[4].Value = model.ProductID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

